package com.tbit.main.controller;

import com.alibaba.fastjson.JSON;
import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.DevicePriority;
import com.tbit.main.service.DevicePriorityService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 功能优先级
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 17:47 2023/3/2
 */
@RestController
@RequestMapping("/devicePriority")
public class DevicePriorityController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private DevicePriorityService devicePriorityService;

    public static void main(String[] args) {
        String json = "[{\"functionId\":1,\"functionName\":\"骑行中\",\"level\":1,\"parentName\":\"车辆使用中\",\"remark\":\"\"},{\"functionId\":2,\"functionName\":\"运维占用\",\"level\":1,\"parentName\":\"车辆使用中\",\"remark\":\"\"},{\"functionId\":3,\"functionName\":\"超区\",\"level\":2,\"parentName\":\"挪车\",\"remark\":\"\"},{\"functionId\":4,\"functionName\":\"站外\",\"level\":2,\"parentName\":\"挪车\",\"remark\":\"\"},{\"functionId\":5,\"functionName\":\"禁停区内\",\"level\":2,\"parentName\":\"挪车\",\"remark\":\"\"},{\"functionId\":6,\"functionName\":\"运维故障\",\"level\":3,\"parentName\":\"不可运营\",\"remark\":\"\"},{\"functionId\":7,\"functionName\":\"无信号\",\"level\":3,\"parentName\":\"不可运营\",\"remark\":\"\"},{\"functionId\":8,\"functionName\":\"电池异常\",\"level\":3,\"parentName\":\"不可运营\",\"remark\":\"\"},{\"functionId\":9,\"functionName\":\"低电\",\"level\":4,\"parentName\":\"换电\",\"remark\":\"\"},{\"functionId\":10,\"functionName\":\"闲置车辆\",\"level\":5,\"parentName\":\"调度\",\"remark\":\"\"},{\"functionId\":11,\"functionName\":\"电池未锁\",\"level\":6,\"parentName\":\"待检修\",\"remark\":\"\"},{\"functionId\":12,\"functionName\":\"摄像异常\",\"level\":6,\"parentName\":\"待检修\",\"remark\":\"\"},{\"functionId\":13,\"functionName\":\"头盔异常\",\"level\":6,\"parentName\":\"待检修\",\"remark\":\"\"},{\"functionId\":14,\"functionName\":\"用户故障\",\"level\":6,\"parentName\":\"待检修\",\"remark\":\"\"},{\"functionId\":15,\"functionName\":\"头盔丢失\",\"level\":6,\"parentName\":\"待检修\",\"remark\":\"\"},{\"functionId\":16,\"functionName\":\"无盈利\",\"level\":6,\"parentName\":\"待检修\",\"remark\":\"\"}]";
        List<DevicePriority> devicePriorities = JSON.parseArray(json, DevicePriority.class);
        Map<String, List<DevicePriority>> map = devicePriorities.stream().collect(Collectors.groupingBy(DevicePriority::getParentName));

        Map<String, List<DevicePriority>> newmap = new HashMap<>();
        for (Map.Entry<String, List<DevicePriority>> entry : map.entrySet()) {
            List<DevicePriority> list = entry.getValue().stream().sorted(Comparator.comparing(DevicePriority::getFunctionId).reversed()).collect(
                    Collectors.toList());
            newmap.put(entry.getKey(), list);
        }
        System.out.println(map);

        System.out.println("-----------------");
        System.out.println(newmap);
    }

    /**
     * 新增
     *
     * @param list  集合对象
     * @param token 鉴权秘钥
     * @return
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.DEVICEPRIORITY, methods = MethodEnum.DEVICEPRIORITY_ADD)
    public JsonResult add(@RequestBody List<DevicePriority> list, String token) {
        Assert.notNull(list, "DevicePriority.list.not.null");
        AccountUser accountUser = tokenService.getAccount(token);
        Integer accountId = list.get(0).getAccountId();
        devicePriorityService.add(list, accountUser, accountId);
        return JsonResult.succ();
    }

    /**
     * 获取区域功能优先级配置
     *
     * @param token     鉴权秘钥
     * @param accountId 区域id
     * @return
     */
    @RequestMapping("/getByAccountId")
    public JsonResult getByAccountId(String token, Integer accountId) {
        AccountUser accountUser = tokenService.getAccount(token);
        if (accountId == null) {
            accountId = accountUser.getAccountId();
        }
        List<DevicePriority> devicePriorities = devicePriorityService.getByAccountId(accountUser, accountId);
        return JsonResult.succ(devicePriorities);
    }
}
